﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Vanfa.Entities.Album;
using Vanfa.Biz.Album;
using Vanfa.Common;
using Vanfa.Web.UI;
using Vanfa.Biz.User;

namespace Vanfa.Web
{
    public partial class show : BasePage
    {
        private string UserName
        {
            get
            {
                if(!string.IsNullOrEmpty(Request["uid"]) )
                    return Request["uid"];
                if (UserData != null)
                    return UserData.UserName;
                return "";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Request["uid"]) && UserData == null)
                Response.Redirect("/index.aspx");
            wucMemberInfo.UserName = UserName;
            if (!Page.IsPostBack)
            {
                this.LoadUserPhoto(UserID);
            }
        }

        private void LoadUserPhoto(int userID)
        {
            repShowPhoto.DataSource = UserPhotoMag.Select(userID);
            repShowPhoto.DataBind();
        }

        protected void repShowPhoto_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var up = e.Item.DataItem as vUserPhoto;
                if (up == null)
                    return;
                var img = e.Item.FindControl("imgPhoto") as Image;
                img.ImageUrl = up.SourcePath;
                img.Width = 235;
                img.Height = 157;
                System.Drawing.Bitmap s = new System.Drawing.Bitmap(Server.MapPath(img.ImageUrl));
                if (s.Width > s.Height)
                {
                    img.Width = 235;
                    img.Height = 157;
                }
                else
                {
                    img.Width = 115;
                    img.Height = 157;
                }
            }
        }

        private int UserID
        {
            get
            {
                if (!string.IsNullOrEmpty(Request["uid"]))
                {
                    var t = UserMag.GetUserTicket(Request["uid"]);
                    if (t == null)
                        return 0;
                    return t.UserID;
                }
                return UserData.UserID;
            }
        }
    }
}